package wwj.controller;

import wwj.bean.HomeData;
import wwj.service.HomePageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

@WebServlet("/HomePageServlet")
public class HomePageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int curPage = 1; //当前页
        int sort = 0;    //表示分类 0是不分类 1是分类

        String curPageParameter = req.getParameter("curPage");

        if (curPageParameter != null) {
            curPage = Integer.parseInt(curPageParameter);
        }
        System.out.println("点击页码:" + curPage);
        String sortParameter = req.getParameter("sort");
        if (sortParameter != null) {
            sort = Integer.parseInt(sortParameter);
        }
        String search_final = "";
        String search_text = req.getParameter("search_text");  // 搜索框数据
        String search_page = req.getParameter("search_page");  // 分页结果查询的原先搜索框数据


        if (search_text != null) {
            search_final = new String(search_text.getBytes("iso-8859-1"), "UTF-8");
        }
        if (search_page!=null){
            search_final = new String(search_page.getBytes("iso-8859-1"), "UTF-8");
        }

        System.out.println("查询数据为：" + search_final);
        HomeData homeData = new HomeData(curPage, sort, search_final);
        HomePageService.prepareHomeData(homeData);

        // 响应
        req.setAttribute("homeData", homeData);
        System.out.println("数据：" + homeData);
        req.getRequestDispatcher("main.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
